package com.api.aiyh_pcn.fadada.dao;

import aiyh.utils.Util;
import aiyh.utils.zwl.common.ToolUtil;
import com.api.aiyh_pcn.fadada.entity.FaDaDaConfigDTO;
import com.api.aiyh_pcn.fadada.entity.PushAPushEmailEntity;
import com.api.aiyh_pcn.fadada.entity.UfContractInfoDTO;
import com.api.aiyh_pcn.fadada.typeEnum.DisableEnableType;
import weaver.aiyh_pcn.fadada.entity.FileInfo;
import weaver.conn.RecordSet;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.List;
import java.util.Map;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/11/3 0003 14:56
 */


public class FaDDContractMapping {
    private final RecordSet rs = new RecordSet();
    private final ToolUtil toolUtil = new ToolUtil();

    /**
     * 查询合同相关的配置信息
     *
     * @param workflowId 流程id
     * @param type       配置的类型
     * @return 配置信息
     */
    public FaDaDaConfigDTO queryConfig(String workflowId, int type) {
        String versionStringByWfid = WorkflowVersion.getVersionStringByWfid(workflowId);
        String query = "select main.id,main.workflow_type,main.api_type, main.params_config," +
                "main.signed_type,wf2.fieldname field_control1," +
                "wf.fieldname field_control, wf1.fieldname contract_doc, " +
                "(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem " +
                "where FIND_IN_SET(id,main.only_own)) only_own, " +
                "(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem " +
                "where FIND_IN_SET(id,main.only_other)) only_other, " +
                "(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem " +
                "where FIND_IN_SET(id,main.both_all)) both_all, " +
                "(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem " +
                "where FIND_IN_SET(id,main.check_personal)) check_personal, " +
                "(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem " +
                "where FIND_IN_SET(id,main.check_enterprise)) check_enterprise," +
                "main.check_source_type,wdt.tablename check_source " +
                "from uf_contract_config  main " +
                "left join workflow_field_table_view  wf on wf.id = main.field_control " +
                "left join workflow_field_table_view  wf1 on wf1.id = main.contract_doc " +
                "left join workflow_field_table_view  wf2 on wf2.id = main.field_control1 " +
                "left join workflow_detail_table_view wdt on wdt.id = main.check_source and " +
                "wdt.workflow_id = main.workflow_type " +
                "where main.workflow_type in ( " + versionStringByWfid + ") and api_type = ?";
        rs.executeQuery(query, type);
        return Util.recordeSet2Entity(rs, FaDaDaConfigDTO.class, true);
    }


    /**
     * 获取全版本的workflowId
     *
     * @param versionStringByWfid 全版本的workflowId
     * @return
     */
    public String getAllVersion(String versionStringByWfid) {
        String query = "select distinct workflow_type from uf_contract_config where workflow_type in (" + versionStringByWfid + ")";
        rs.executeQuery(query);
        rs.next();
        return rs.getString(1);
    }

    /**
     * 获取节点信息
     *
     * @param versionStringByWfid 全版本的流程
     * @param markOnly            获取配置的节点信息
     * @return
     */
    public String getNodes(String versionStringByWfid) {
        String query = "select workflow_nodes from uf_node_config where workflow_type in (" + versionStringByWfid + ") and enable_nodes = ?";
        rs.executeQuery(query, 1);
        rs.next();
        return rs.getString(1);
    }

    /**
     * 获取流程主表
     *
     * @param workflowId
     * @return
     */
    public String getMainTable(String workflowId) {
        String versionStringByWfid = WorkflowVersion.getVersionStringByWfid(workflowId);
        String query = "select tablename from workflow_bill " +
                " where id in (select formid from workflow_base " +
                " where id in (" + versionStringByWfid + ") )";
        rs.executeQuery(query);
        rs.next();
        String mainTable = rs.getString(1);
        toolUtil.writeDebuggerLog("mainTable:" + mainTable);
        return mainTable;
    }

    /**
     * 获取配置的明细表
     *
     * @param workflowId
     * @param type
     * @return
     */
    public String getDetailTable(String workflowId, int type) {
        FaDaDaConfigDTO faDaDaConfigDTO = this.queryConfig(workflowId, type);
        String detailTable = faDaDaConfigDTO.getCheckSource();
        toolUtil.writeDebuggerLog("detailTable:" + detailTable);
        return detailTable;
    }

    /**
     * 根据主表和请求id查询主表数据
     *
     * @param requestId     请求id
     * @param mainTableName 主表表名
     * @return
     */
    public Map<String, Object> queryMainMap(String requestId, String mainTableName) {
        String query = "select * from " + mainTableName + " where requestid = ?";
        RecordSet rs = new RecordSet();
        rs.executeQuery(query, requestId);
        return Util.recordSet2Map(rs);
    }

    /**
     * 查询明细数据
     *
     * @param mainId    主表id
     * @param tableName 明细表表名
     * @return
     */
    public List<Map<String, Object>> queryDetailMaps(String mainId, String tableName) {
        String query = "select * from " + tableName + " where mainid = ?";
        RecordSet rs = new RecordSet();
        rs.executeQuery(query, mainId);
        return Util.recordSet2MapList(rs);
    }


    /**
     * 查询合同的配置信息
     *
     * @param workflowId
     * @return
     */
/*	public Map<String, Object> queryContractConfig(String workflowId) {
		String versionStringByWfid = WorkflowVersion.getVersionStringByWfid(workflowId);
		RecordSet rs = new RecordSet();
		rs.executeQuery("select * from uf_contract_config where workflow_type in ( " + versionStringByWfid + " ) and api_type = ?", 2);
		return Util.recordSet2Map(rs);
	}*/

    /**
     * 根据文件的id查询物理文件的id
     *
     * @param fileIds
     * @return
     */
    public List<FileInfo> queryImgFileIdByDocIds(String fileIds) {
        String query = "select imagefileid,imagefilename from docimagefile where docid in ( " + fileIds + " )";
        rs.executeQuery(query);
        return Util.recordeSet2Array(rs, FileInfo.class);
    }

    public UfContractInfoDTO queryContractInfo(String contractNo) {
        String query = "select * from uf_contract_info where contract_no = ?";
        RecordSet rs = new RecordSet();
        rs.executeQuery(query, contractNo);
        return Util.recordeSet2Entity(rs, UfContractInfoDTO.class, true);
    }

    public UfContractInfoDTO queryContractInfoByRequestId(String requestId) {
        String query = "select * from uf_contract_info where workflow_request_id = ?";
        RecordSet rs = new RecordSet();
        rs.executeQuery(query, requestId);
        return Util.recordeSet2Entity(rs, UfContractInfoDTO.class, true);
    }

    public PushAPushEmailEntity queryEmailInfo(int workflowType) {
        String query = "select * from uf_push_a_push where workflow_type = ? and disable_enable = ?";
        RecordSet rs = new RecordSet();
        rs.executeQuery(query, workflowType, DisableEnableType.enable.getValue());
        return Util.recordeSet2Entity(rs, PushAPushEmailEntity.class, true);
    }
}
